package com.guli.gulimall.test.conf;


import java.util.concurrent.*;

/**
 * @author WZH
 * @title: MyTask
 * @projectName gulimall-java
 * @description:
 * @date 2022/1/11
 */
public class MyTask {
    //线程是异步的
    //创建线程池
    private static ThreadPoolExecutor threadPool = new ThreadPoolExecutor(3,
            20,
            30,
            TimeUnit.SECONDS,
            new LinkedBlockingDeque<Runnable>(100),
            Executors.defaultThreadFactory(),
            new ThreadPoolExecutor.AbortPolicy());

    public static void  main(String[] arg) throws ExecutionException, InterruptedException {
        System.out.println("主线程开始");
        //匿名内部类创建单一线程
//        new Thread(()->{
//            System.out.println(Thread.currentThread().getName()+"开始了");
//            //业务
//            System.out.println(Thread.currentThread().getName()+"结束了");
//        }).start();
//
//        new Thread(new Thread1()).start();
//        //线程池处理线程
//        threadPool.submit(()->{
//            System.out.println(Thread.currentThread().getName()+"开始了");
//            //业务
//            System.out.println(Thread.currentThread().getName()+"结束了");
//        });
        //CompletableFuture 线程编排
        //无返回值线程
        CompletableFuture.runAsync(()->{
            System.out.println(Thread.currentThread().getName()+"开始了");
            //业务
            System.out.println(Thread.currentThread().getName()+"结束了");
        },threadPool);
        //有返回值线程
        CompletableFuture<Integer> supplyAsync = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "开始了");
            //业务
            System.out.println(Thread.currentThread().getName() + "结束了");
            return 1;
        }).handleAsync((t,e)->{//回调方法  获取返回值和捕获异常并且可返回给主函数
            System.out.println("捕获上级返回值："+t+"异常："+e);
            return -1;},threadPool);
        //串行化
        CompletableFuture<Void> future = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "1开始了");
            Integer i = 10 / 2;
            System.out.println(Thread.currentThread().getName() + "1结束了");
            return i;
        }, threadPool).thenApplyAsync((data) -> {//有参有返
            System.out.println(Thread.currentThread().getName() + "2开始了" + data);
            System.out.println(Thread.currentThread().getName() + "2结束了");
            return data;
        }, threadPool).thenAcceptAsync((data) -> {//有参无返
            System.out.println(Thread.currentThread().getName() + "3开始了" + data);
            System.out.println(Thread.currentThread().getName() + "3结束了");
        }, threadPool).thenRunAsync(() -> {//无参无返
            System.out.println(Thread.currentThread().getName() + "4开始了");
            System.out.println(Thread.currentThread().getName() + "4结束了");
        }, threadPool);
        //多任务组合 参数为数组可多添加 allOf等待全部线程走完才往下走 anyOf只要有一个任务完成就往下走
        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.allOf(supplyAsync, future);
        //supplyAsync.get() 阻塞方法当voidCompletableFuture线程完成时，主线程才会结束
        System.out.println("主线程结束"+voidCompletableFuture.get());


    }
}
